<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/23
 * Time: 16:38
 */
namespace app\notification\behavior;

/**
 * Class ProductOrder
 * 产品订单行为
 * @package app\notification\behavior
 */
class ProductOrder
{

    protected $job = 'app\notification\command\TemplateMessage';

    public function run(&$params){}

    /**
     * 下单成功后 && 通过初核
     *  推送站内信、微信模板消息给对应的人
     *    如果有客户经理，则推送到个人池，对象为所属客户经理；
     *    如果没有客户经理，则推送公共池，对象为所有客户经理；
     * @param $params
     */
    public function ApplyAfter(&$params) {
        if($params['stage'] !== 0) return;

        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');

        $replace = [
            '{{客户姓名}}' => (string) $params->realname,
            '{{产品类型}}' => (string) $params->product->name,
        ];

        if($params['customer_id'] > 0) {
            $notification = $lang['客户进入个人池'];
            $replace['{{客户经理姓名}}'] = $params->customer_info->customer_realname;
            if($notification['message']) {
                $title = $notification['message']['title'];
                $content = str_replace(array_keys($replace), $replace, $notification['message']['content']);
                model('message/message', 'service')
                    ->send($params->customer_id, $title, $content, $params->uid, [
                        'role' => 2,
                        'order_sn' => $params->sn,
                        'target' => '/pages/customer/product_order/item?sn='.$params->sn
                    ]);
            }

            $replace['{{跟单时限}}'] = config('product.product_order_flow_interval');
            if ($notification['sms'] && $params->customer_info->mobile) {
                $template_id = $notification['sms']['template_id'];
                $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
                model('admin/dysms', 'service')->send($params->customer_info->mobile, $template_vars, $template_id);
            }

//            if($notification['template_message']) {
//                $fans = $params->customer_info->getAttr('wechat_fans');
//                if(!$fans) return;
//                $mini_program = config('wechat.mini_program');
//                $data = [
//                    'touser' => $fans['openid'],
//                    'miniprogram' => [
//                        'appid' => $mini_program['app_id'],
//                        'pagepath' => '/pages/customer/product_order/item?sn='.$params->sn,
//                    ],
//                    'template_id' => $notification['template_message']['template_id'],
//                    'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//                ];
//                queue('app\notification\command\TemplateMessage', $data, 0);
//            }




        } else {
            $notification = $lang['客户进入公共池'];
            model('user/user')->where([
                'role'  => 2,
                'status' => 1,
            ])->chunk(100, function($users) use (&$notification, &$replace, &$params) {
                foreach ($users as $user) {
                    if($notification['template_message']) {
                        $replace['{{客户经理姓名}}'] = (string) $user->customer_realname;
                        $title = $notification['message']['title'];
                        $content = str_replace(array_keys($replace), $replace, $notification['message']['content']);
                        model('message/message', 'service')
                            ->send($user['id'], $title, $content, $params->uid, [
                                'role' => 2,
                                'order_sn' => $params->sn,
                                'target' => '/pages/customer/customer/customer?currentTab=1'
                            ]);
                    }

                    if ($notification['sms'] && $user->mobile) {
                        $replace['{{客户经理姓名}}'] = $user['customer_realname'];
                        $template_id = $notification['sms']['template_id'];
                        $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
                        model('admin/dysms', 'service')->send($mobile, $template_vars, $template_id);
                    }

//                    if($notification['template_message']) {
//                        $fans = $user->getAttr('wechat_fans');
//                        if(!$fans) continue;
//                        $replace['{{客户经理姓名}}'] = $user['customer_realname'];
//                        $mini_program = config('wechat.mini_program');
//                        $data = [
//                            'touser' => $fans['openid'],
//                            'miniprogram' => [
//                                'appid' => $mini_program['app_id'],
//                                'pagepath' => '/pages/customer/product_order/item?sn='.$params->sn,
//                            ],
//                            'template_id' => $notification['template_message']['template_id'],
//                            'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//                        ];
//                        queue('app\notification\command\TemplateMessage', $data, 0);
//                    }
                }
            });
        }
        return;
    }

    /**
     * 客户经理完成首次联系
     * @param $params
     */
    public function ContactAfter(&$params) {

        //完成首次联系
        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');
        $notification = $lang['完成首次联系'];

        $replace = [
            '{{客户姓名}}' => (string) $params->realname,
            '{{产品类型}}' => (string) $params->product->name,
        ];

        if ($notification['sms'] && $params->mobile) {
            $template_id = $notification['sms']['template_id'];
            $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
            model('admin/dysms', 'service')->send($params->mobile, $template_vars, $template_id);
        }

//        if($notification['template_message']) {
//            $mini_program = config('wechat.mini_program');
//            $data = [
//                'touser' => $fans['openid'],
//                'miniprogram' => [
//                    'appid' => $mini_program['app_id'],
//                    'pagepath' => '/pages/product/order/item?sn='.$params->sn,
//                ],
//                'template_id' => $notification['template_message']['template_id'],
//                'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//            ];
//            queue('app\notification\command\TemplateMessage', $data, 0);
//        }
    }


    /**
     * 完成资料清单
     * @param $params
     */
    public function ProfileAfter(&$params) {
        if(!$params->customer_info) return;

        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');
        $notification = $lang['完成资料清单'];

        $replace = [
            '{{客户姓名}}' => (string) $params->realname,
            '{{产品类型}}' => (string) $params->product->name,
        ];


        if($notification['message']) {
            $title = $notification['message']['title'];
            $content = str_replace(array_keys($replace), $replace, $notification['message']['content']);
            model('message/message', 'service')
                ->send($params->customer_id, $title, $content, $params->uid, [
                    'role' => 2,
                    'order_sn' => $params->sn,
                    'target' => '/pages/customer/product_order/item?sn='.$params->sn
                ]);
        }

        if ($notification['sms'] && $params->customer_info->mobile) {
            $template_id = $notification['sms']['template_id'];
            $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
            model('admin/dysms', 'service')->send($params->customer_info->mobile, $template_vars, $template_id);
        }


//        if($notification['template_message'] && $params->customer_info->getAttr('wechat_fans')) {
//            $mini_program = config('wechat.mini_program');
//            $data = [
//                'touser' => $fans['openid'],
//                'miniprogram' => [
//                    'appid' => $mini_program['app_id'],
//                    'pagepath' => '/pages/customer/product_order/item?sn='.$params->sn,
//                ],
//                'template_id' => $notification['template_message']['template_id'],
//                'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//            ];
//            queue('app\notification\command\TemplateMessage', $data, 0);
//        }
    }

    /**
     * 录入贷款审批结果后
     * @param $params
     * @return mixed
     */
    public function ReviewAfter(&$params) {
        if(!$params->user) return;

        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');

        if($params->stage == 99) {
            $notification = $lang['贷款审批成功'];
        } else {
            $notification = $lang['贷款审批失败'];
        }

        $replace = [
            '{{客户姓名}}' => (string) $params->realname,
            '{{产品类型}}' => (string) $params->product->name,
        ];

        if ($notification['sms'] && $params->mobile) {
            $template_id = $notification['sms']['template_id'];
            $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
            model('admin/dysms', 'service')->send($params->mobile, $template_vars, $template_id);
        }

//        if($notification['template_message'] && $params->user->getAttr('wechat_fans')) {
//            $fans = $params->user->getAttr('wechat_fans');
//            $mini_program = config('wechat.mini_program');
//            $data = [
//                'touser' => $fans['openid'],
//                'miniprogram' => [
//                    'appid' => $mini_program['app_id'],
//                    'pagepath' => '/pages/product/order/item?sn='.$params->sn,
//                ],
//                'template_id' => $notification['template_message']['template_id'],
//                'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//            ];
//            queue('app\notification\command\TemplateMessage', $data, 0);
//        }
        return;
    }

    /**
     * 跟单时限提醒
     * @param $params
     */
    public function FlowMessageAfter() {
        \think\Lang::load(APP_PATH . 'notification/lang/zh-cn.php');
        $lang = \think\Lang::get('notification');

        $notification = $lang['跟进时限提醒'];

        $product_order_flow_interval = config('product.product_order_flow_interval');
        $product_order_flow_message = $product_order_flow_interval - config('product.product_order_flow_message');

        $min_create_time = time() - $product_order_flow_message * 60;
        $max_create_time = time() - $product_order_flow_interval * 60;

        model('product/product_order')->where([
            'status' => 0,
            'stage' => 0,
            'create_time' => ['between', [$min_create_time, $max_create_time]],
            'customer_id'  => ['gt', 0],
            'follow_notification' => 0,
        ])->chunk(100, function($items) use (&$notification, &$replace, &$params) {
            foreach ($items as $item) {
                $replace = [
                    '{{客户姓名}}' => (string) $item->realname,
                    '{{产品类型}}' => (string) $item->product->name,
                    '{{跟进提醒时间}}' => config('product.product_order_flow_message')
                ];

                if($notification['message']) {
                    $replace['{{客户经理姓名}}'] = (string) $item->customer_info->customer_realname;
                    $title = $notification['message']['title'];
                    $content = str_replace(array_keys($replace), $replace, $notification['message']['content']);
                    model('message/message', 'service')
                        ->send($item->customer_id, $title, $content, $item->uid, [
                            'role' => 2,
                            'order_sn' => $item->sn,
                            'target' => '/pages/customer/product_order/item?sn='.$item->sn
                        ]);
                }

                if ($notification['sms'] && $item->customer_info->mobile) {
                    $template_id = $notification['sms']['template_id'];
                    $template_vars = str_replace(array_keys($replace), $replace, $notification['sms']['template_vars']);
                    model('admin/dysms', 'service')->send($params->customer_info->mobile, $template_vars, $template_id);
                }

//                if($notification['template_message']) {
//                    $fans = $item->customer_info->getAttr('wechat_fans');
//                    if(!$fans) continue;
//                    $replace['{{客户经理姓名}}'] = $fans['customer_realname'];
//                    $mini_program = config('wechat.mini_program');
//                    $data = [
//                        'touser' => $fans['openid'],
//                        'miniprogram' => [
//                            'appid' => $mini_program['app_id'],
//                            'pagepath' => '/pages/customer/product_order/item?sn='.$item->sn,
//                        ],
//                        'template_id' => $notification['template_message']['template_id'],
//                        'data' => str_replace(array_keys($replace), $replace, $notification['template_message']['template_vars'])
//                    ];
//                    queue('app\notification\command\TemplateMessage', $data, 0);
//                }

                // 更新状态
                $item->follow_notification = 1;
                $item->save();

            }
        });
    }

}
